package jhavelogger.parser;

import java.util.HashSet;

import jhavelogger.models.AcceptedConnection;
import jhavelogger.models.SendAlgorithmToClient;
import jhavelogger.models.UsernameLookup;
import jhavelogger.models.UnparsedLog;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;

public class SendAlgorithmToClientFactoryTests extends LogFactoryTestingBase {

    private HashSet<LogMessageType> allowedLogMessageTypes;

    @Before
    public void setUp() throws Exception {
	super.setUp();
	this.factory = new SendAlgorithmToClientFactory();
	this.allowedLogMessageTypes = new HashSet<LogMessageType>();
	this.allowedLogMessageTypes.add(LogMessageType.SENDALGORITHM);
	this.allowedLogMessageTypes.add(LogMessageType.SENDALGORITHMWITHSUBALGORITHM);
    }

    @Test
    public void setsAllNeededPropertiesWithSubAlgorithm() {
	String line = "3/20/11 8:01 PM - FINE: sendtoClient - 400 memorymanager/exercise17.cp gaigs";
	SendAlgorithmToClient log = (SendAlgorithmToClient) this.factory.create(new UnparsedLog("127.0.0.1", line));
	
	Assert.assertNotNull("A log object must be created", log);
	Assert.assertEquals("Algorithm must match expected value", "memorymanager/exercise17.cp", log.getAlgorithm());
	// Subtract 1900 from the year... It's a java thing
	Assert.assertEquals("Year must match expected value", 111, log.getDate().getYear());
	// zero based months
	Assert.assertEquals("Month must match expected value", 2, log.getDate().getMonth());
	Assert.assertEquals("Date match expected value", 20, log.getDate().getDate());
	Assert.assertEquals("Hour match expected value", 20, log.getDate().getHours());
	Assert.assertEquals("Minute match expected value", 1, log.getDate().getMinutes());
    }
    
    @Test
    public void setsAllNeededPropertiesWithNoAlgorithm() {
	String line = "3/20/11 8:01 PM - FINE: sendtoClient - 400 sutherland gaigs";
	SendAlgorithmToClient log = (SendAlgorithmToClient) this.factory.create(new UnparsedLog("127.0.0.1", line));
	
	Assert.assertNotNull("A log object must be created", log);
	Assert.assertEquals("Algorithm must match expected value", "sutherland", log.getAlgorithm());
	// Subtract 1900 from the year... It's a java thing
	Assert.assertEquals("Year must match expected value", 111, log.getDate().getYear());
	// zero based months
	Assert.assertEquals("Month must match expected value", 2, log.getDate().getMonth());
	Assert.assertEquals("Date match expected value", 20, log.getDate().getDate());
	Assert.assertEquals("Hour match expected value", 20, log.getDate().getHours());
	Assert.assertEquals("Minute match expected value", 1, log.getDate().getMinutes());
    }
    
    @Test
    public void parsesSendAlgorithmLogs() {
	Assert.assertTrue(this.factoryCreatesObject(LogMessageType.SENDALGORITHM));
	Assert.assertTrue(this.factoryCreatesObject(LogMessageType.SENDALGORITHMWITHSUBALGORITHM));
    }

    @Test
    public void doesNotParseOtherLogs() {
	Assert.assertNull(this.factoryDoesNotCreateObjectsOfTypesOtherThan(this.allowedLogMessageTypes));
    }
}
